/**
 * Copyright (C) 2017 - 2020 https://github.com/joewee
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.bifrost.cbrepository;

import com.bifrost.cbentity.SapContractProduct;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author joewee
 * @version 1.0.0
 * @Date 2018/3/7 13:22
 */
public interface SapContractProductRepository extends JpaRepository<SapContractProduct, Long> {
    List<SapContractProduct> findAllByDstatus(String status);

    List<SapContractProduct> findAllByDstatus(String status, Pageable pageable);

    @Modifying
    @Transactional
    @Query(value = "UPDATE BHWSC_GHSP SET DSTATUS = '1' WHERE NOID = ?1",nativeQuery = true)
    int updateStatusToSuccess(Long noid);

    /**
     *注意！！！该方法返回的实体只包含zzghn属性
     * @param externalId
     * @param storeCode 门店代码
     * @return
     */
    //@Query("select new SapContractProduct(s.zzghn) from SapContractProduct s where s.matnr =?1 and s.werks =?2 and s.delbs is null")
    List<SapContractProduct> findAllByMatnrAndWerksAndDelbsIsNull(String externalId, String storeCode);

    SapContractProduct findFirstByMatnrAndWerksAndDelbsIsNullOrderByNoidDesc(String externalId, String storeCode);

}
